package com.cbgolf.oa.util;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import java.util.List;

/**
 * Created by lq on 2018/5/14.
 */

public class ServiceUtil {

  /**
   * 用来判断某个服务是否开启
   */
  public static boolean isServiceRunning(Context mContext, String className) {
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager)
        mContext.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> serviceList
        = activityManager.getRunningServices(30);
    if (!(serviceList.size() > 0)) {
      return false;
    }
    for (int i = 0; i < serviceList.size(); i++) {
      if (serviceList.get(i).service.getClassName().equals(className) == true) {
        isRunning = true;
        break;
      }
    }
    return isRunning;
  }


  /**
   * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
   *
   * @return true 表示开启
   */
  public static final boolean isOPen(final Context context) {
    LocationManager locationManager
        = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
    boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
    boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (gps || network) {
      return true;
    }

    return false;
  }

  /**
   * 强制帮用户打开GPS
   */
  public static final void openGPS(Context context) {
    try {
      Intent GPSIntent = new Intent();
      GPSIntent.setClassName("com.android.settings",
          "com.android.settings.widget.SettingsAppWidgetProvider");
      GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
      GPSIntent.setData(Uri.parse("custom:3"));
      PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
    } catch (PendingIntent.CanceledException e) {
      e.printStackTrace();
    }
  }

}
